package ods_supplier

import (
	"server/global"
	"server/model/common/request"
)

type SupplierService struct {
}

func (odsSupplierService *SupplierService) CreateOdsSupplier(odsSupplier *OdsSupplier) (err error) {
	err = global.SysDB.Create(odsSupplier).Error
	return err
}

func (odsSupplierService *SupplierService) DeleteOdsSupplier(odsSupplier OdsSupplier) (err error) {
	err = global.SysDB.Delete(&odsSupplier).Error
	return err
}

func (odsSupplierService *SupplierService) DeleteOdsSupplierByIds(ids request.IdsReq) (err error) {
	err = global.SysDB.Delete(&[]OdsSupplier{}, "id in ?", ids.Ids).Error
	return err
}

func (odsSupplierService *SupplierService) UpdateOdsSupplier(odsSupplier OdsSupplier) (err error) {
	err = global.SysDB.Save(&odsSupplier).Error
	return err
}

func (odsSupplierService *SupplierService) GetOdsSupplierById(id uint) (odsSupplier OdsSupplier, err error) {
	err = global.SysDB.Where("id = ?", id).First(&odsSupplier).Error
	return
}

func (odsSupplierService *SupplierService) GetOdsSupplierInfoList(info OdsSupplierSearch) (list []OdsSupplier, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&OdsSupplier{})
	var odsSuppliers []OdsSupplier
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&odsSuppliers).Error
	return odsSuppliers, total, err
}

func (odsSupplierService *SupplierService) GetAllOdsSuppliers() (odsSupplier []OdsSupplier, err error) {
	err = global.SysDB.Find(&odsSupplier).Error
	return odsSupplier, err
}
